package com.brush.web.anonymous;

import com.brush.common.constant.CacheConstants;
import com.brush.common.core.domain.R;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.SpecCaptcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 图片验证码
 * @author admin
 */
@Api(tags = "匿名访问-验证码")
@RestController
@RequestMapping("/anonymous/captcha")
public class EasyCaptchaController {

    /**
     * spring自动装配对象
     */
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 生成png格式的验证码
     * @return
     */
    @ApiOperation(value = "生成png格式的验证码")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "width", value = "验证码图片宽度"),
            @ApiImplicitParam(name = "height", value = "验证码图片高度"),
            @ApiImplicitParam(name = "len", value = "验证码位数")
    })
    @GetMapping("/png")
    public R<Map<String,String>>  png(@RequestParam(name = "width", defaultValue = "150") int width,
                                      @RequestParam(name = "height", defaultValue = "50") int height,
                                      @RequestParam(name = "len", defaultValue = "4") int len){
        SpecCaptcha specCaptcha = new SpecCaptcha(width, height, len);

        // 验证码的结果
        String verCode = specCaptcha.text().toLowerCase();
        // 得到验证码图片 转为Base64字符串
        String image_base64=specCaptcha.toBase64();

        // 验证码的唯一标识
        String key = UUID.randomUUID().toString();

        Map<String,String> res=new HashMap<>();
        res.put("img",image_base64);
        res.put("uuid", key);

        // 存入redis并设置过期时间为5分钟
        String redisKey= CacheConstants.CAPTCHA_CODE_KEY+key;
        this.redisTemplate.opsForValue().set(redisKey,verCode,5, TimeUnit.MINUTES);

        return R.ok(res);
    }


    /**
     * 生成airth格式的验证码
     * @return
     */

    @ApiOperation(value = "生成算术运算的验证码")
    @GetMapping("/airth")
    public R<Map<String,String>>  arithmeticCaptcha (@RequestParam(name = "width", defaultValue = "150") int width,
                                      @RequestParam(name = "height", defaultValue = "50") int height,
                                      @RequestParam(name = "len", defaultValue = "2") int len){
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(width, height, len);

        // 验证码的结果
        String verCode = captcha.text().toLowerCase();
        // 得到验证码图片 转为Base64字符串
        String image_base64=captcha.toBase64();

        // 验证码的唯一标识
        String key = UUID.randomUUID().toString();

        Map<String,String> res=new HashMap<>();
        res.put("img",image_base64);
        res.put("uuid", key);

        // 存入redis并设置过期时间为5分钟
        String redisKey= CacheConstants.CAPTCHA_CODE_KEY+key;
        this.redisTemplate.opsForValue().set(redisKey,verCode,5, TimeUnit.MINUTES);

        return R.ok(res);
    }


}
